package verify

import (
	"errors"
	"fmt"

	"awesomeProject/app/request"
	"awesomeProject/app/service"
)

func RoleCreate(r *request.RoleCreate, companyId uint) error {
	if r.Name == "" {
		return errors.New("名称（name）不能为空")
	}
	if r.Alias == "" {
		return errors.New("别名（alias）不能为空")
	}
	// 唯一性校验
	var roleSrv service.RoleSrv
	aliasExist := roleSrv.FindByAlias(r.Alias)
	if aliasExist.ID > 0 {
		return errors.New(fmt.Sprintf("别名（%s）已被使用", r.Alias))
	}

	return nil
}

func RoleUpdate(r *request.RoleUpdate) error {
	if r.Name == "" {
		return errors.New("名称不能为空")
	}
	if r.Alias == "" {
		return errors.New("别名不能为空")
	}
	return nil
}

func RolePagination(r *request.RolePagination) error {
	if r.PageIndex == 0 {
		r.PageIndex = 1
	}
	if r.PageSize == 0 {
		r.PageSize = 20
	}

	return nil
}

func AddMenusForRole(r *request.AssignMenus) error {
	if len(r.Checked) == 0 && len(r.HalfChecked) == 0 {
		return errors.New("请求参数错误！")
	}

	return nil
}

func AddShopsForRole(r *request.AssignShops) error {
	fmt.Println(r.ShopIds)
	if len(r.ShopIds) == 0 {
		return errors.New("请至少选择一个店铺！")
	}

	return nil
}
